package com.anjubao.parking.municipal.common.global;

import java.lang.annotation.Annotation;
import java.util.Map;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

@Component
public class AppContext implements ApplicationContextAware {
	private static ApplicationContext applicationContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		AppContext.applicationContext = applicationContext;

	}

	public static <T> T getBean(Class<T> clasz) {
		return applicationContext != null ? applicationContext.getBean(clasz) : null;
	}

	public static <T> Map<String, T> getBeanBySuper(Class<T> clasz) {
		return applicationContext != null ? applicationContext.getBeansOfType(clasz) : null;
	}

	public static Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) {
		return applicationContext != null ? applicationContext.getBeansWithAnnotation(annotationType) : null;
	}

}
